import serial
import time

# 配置串口
ser = serial.Serial(
    port='/dev/ttyAMA0',  # 树莓派的串口设备文件
    baudrate=9600,  # 波特率
    bytesize=serial.EIGHTBITS,  # 数据位
    parity=serial.PARITY_NONE,  # 无校验位
    stopbits=serial.STOPBITS_ONE  # 停止位
)

try:
    while True:
        # 发送数据
        send_data = 'Hello, STM32!'
        ser.write(send_data.encode())
        print(f'Sent: {send_data}')

        time.sleep(1)

except KeyboardInterrupt:
    print('Communication stopped by user.')
finally:
    ser.close()